//
//  AppDelegate.swift
//  LRWeibo
//
//  Created by admin on 15/9/21.
//  Copyright © 2015年 懒人. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.rootViewController = defaultViewController()
        window?.makeKeyAndVisible()
        
        setAppearence()
        
        // 注册通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "rootViewControllChange:", name: LRRootViewControllChange, object: nil)
        
        return true
    }
    
    // 摧毁控制器
    deinit
    {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    /// 更新根控制器
    @objc private func rootViewControllChange(notification: NSNotification)
    {
        let vc = notification.object == nil ? LRWelcomeViewController() : LRMainViewController()
        
        window?.rootViewController = vc
    }
    
    /// 默认根控制器
    private func defaultViewController()->UIViewController
    {
        // 是否登录
        if UserAccountViewModel.sharedUserAccount.isLogin
        {
            return isNewVersion() ? LRNewFeatureViewController() : LRWelcomeViewController()
        }
        
        // 是否第一次使用
        if NSUserDefaults.standardUserDefaults().doubleForKey("sandBoxVerson") == 0{
            return LRNewFeatureViewController()
        }

       // 没登录则到主页面登录
        return LRMainViewController()
    }

    /// 判断是否新版本->显示新特性
    private func isNewVersion()->Bool
    {
        // 获取当前版本 和 历史版本 对比
        let currentVersion = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as! String
        let sandBoxVersion = NSUserDefaults.standardUserDefaults().doubleForKey("sandBoxVersion")
        
        if Double(currentVersion) > sandBoxVersion
        {
            // 保存当前版本
            NSUserDefaults.standardUserDefaults().setDouble(Double(currentVersion)!, forKey: "sandBoxVersion")
            return true
        }

        return false
    }
    
    /// 设置显示颜色
    private func setAppearence()
    {
        UITabBar.appearance().tintColor = WBApperenceColor
        UINavigationBar.appearance().tintColor = WBApperenceColor
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

